package cn.edu.scau.cmi.chenbingquan.comprehension.dao;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.LockOptions;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import cn.edu.scau.cmi.chenbingquan.comprehension.domain.Student;
import cn.edu.scau.cmi.chenbingquan.comprehension.util.HibernateSessionFactoryUtil;

// 对学生的操作
public class StudentDAO {
	private static final Logger log = LoggerFactory.getLogger(StudentDAO.class);
	public static final String NAME = "name";		// property constants
	private Session session;		//	dao依赖hibernate session对象;使用spring封装该属性
	
	public Session getSession() {
		session = HibernateSessionFactoryUtil.getSession();
		return session;
	}
	
	public void save(Student student) {
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.save(student);
			transaction.commit();
			session.close();
		} catch(RuntimeException re) {
			throw re;
		}
	}

	public void delete(Student student) {
		try {
			getSession();
			Transaction transaction = session.beginTransaction();
			session.delete(student);
			transaction.commit();
			session.close();
		} catch(RuntimeException re) {
			throw re;
		}
	}

	// 修改
	public void update(Student student) {
		getSession();
		Transaction transaction = session.beginTransaction();
			
		Student temp = (Student)session.get(Student.class, student.getId());
		temp.setName(student.getName());
		transaction.commit();
		session.close();
	}
	
	// 学生选择老师(无老师时才能选择老师)
	public void selectTeacher(Student student) {
		getSession();
		Transaction transaction = session.beginTransaction();
				
		Student temp = (Student)session.get(Student.class, student.getId());
		if(temp.getTeacher() == null) {
			temp.setTeacher(student.getTeacher());
		}
		transaction.commit();
		session.close();
	}
		
	public Student findById(java.lang.Long id) {
		log.debug("getting Student instance with id: " + id);
		try {
			Student instance = (Student)getSession().get("cn.edu.scau.cmi.chenbingquan.comprehension.domain.Student", id);
			return instance;
		} catch(RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Student instance) {
		log.debug("finding Student instance by example");
		try {
			List results = getSession().createCriteria("cn.edu.scau.cmi.chenbingquan.comprehension.domain.Student")
										.add(Example.create(instance)).list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch(RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Student instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Student as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findAll() {
		log.debug("finding all Student instances");
		try {
			String queryString = "from Student";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch(RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*	将传入的detached状态的对象的属性复制到持久化对象中，并返回该持久化对象 。
	 * 	如果该session中没有关联的持久化对象，加载一个，如果传入对象未保存，保存一个副本并作为持久对象返回，
	 * 	传入对象依然保持detached状态
	 */
	public Student merge(Student detachedInstance) {
		log.debug("merging Student instance");
		try {
			Student result = (Student) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*	将传入的对象持久化并保存。如果对象未保存(Transient状态)，调用save方法保存。
	 * 	如果对象已保存(Detached状态)，调用update方法将对象与Session重新关联。
	 */
	public void attachDirty(Student instance) {
		log.debug("attaching dirty Student instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	// 将传入的对象状态设置为Transient状态(瞬时状态)
	public void attachClean(Student instance) {
		log.debug("attaching clean Student instance");
		try {
			getSession().buildLockRequest(LockOptions.NONE).lock(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}